﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Mindfor.Web.Data;
using Mindfor.Web.Models;
using Mindfor.Web.Routing;
using NHibernate.Linq;
using NHibernate;

namespace Mindfor.Web.Modules.System.Controllers
{
	/// <summary>
	/// Class allows manipulations with site languages.
	/// </summary>
	[CmsAuthorize(CmsRoles.Administrator)]
	public class LanguagesController : CmsController
	{
		/// <summary>
		/// Shows current languages.
		/// </summary>
		[AdminLink("Языки", "Системные")]
		[UrlRoute(Path="Admin/Languages")]
		public ActionResult Index()
		{
			ViewData.Model = Data.Query<Language>().ToList();
			return View();
		}

		/// <summary>
		/// Shows language add view.
		/// </summary>
		[UrlSubAction("Index")]
		[UrlRoute("Add", Path="Admin/Languages")]
		[LoadModelState]
		public ActionResult Add()
		{
			return Edit(null);
		}

		/// <summary>
		/// Saves new language.
		/// </summary>
		/// <param name="model">Language model.</param>
		[HttpPost]
		[UrlRouteSkip]
		public ActionResult Add(Language model)
		{
			return Edit(null, model);
		}

		/// <summary>
		/// Shows language edit view.
		/// </summary>
		/// <param name="id">Language identity.</param>
		[UrlSubAction("Index")]
		[UrlRoute("{id}/Edit", Path="Admin/Languages")]
		[UrlRouteConstraint("id", Constraints.IsInt)]
		[LoadModelState]
		public ActionResult Edit(int? id)
		{
			// get language
			if (id.HasValue)
			{
				Language lang = Data.Get<Language>(id);
				if (lang == null)
					return HttpNotFound();
				ViewData.Model = lang;
			}
			
			// cultures
			ViewBag.Cultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
				.OrderBy(o => o.DisplayName)
				.Select(ci => new SelectListItem
				{
					Text = ci.DisplayName,
					Value = ci.TextInfo.CultureName
				});

			ViewBag.CancelUrl = Url.Action("Index");
			return View("Edit");
		}

		/// <summary>
		/// Saves edited language to DB.
		/// </summary>
		/// <param name="id">Language identity.</param>
		/// <param name="model">Language model.</param>
		[HttpPost]
		[UrlRouteSkip]
		public ActionResult Edit(int? id, Language model)
		{
			// validate
			if (ModelState.IsValid)
			{
				model = Data.MergeEntity(model);
				if (id.HasValue && model.Id != id.Value)
					return HttpNotFound();

				using (ITransaction t = Data.BeginTransaction())
				{
				    Data.SaveOrUpdate(model);
				    t.Commit();
				}
				return RedirectToAction("Index");
			}
			else
			{
				// return to edit view
				this.SaveModelState();
				return RedirectToAction(id == null ? "Add" : "Edit", new { id });
			}

		}
		
		/// <summary>
		/// Deletes language from DB.
		/// </summary>
		/// <param name="id">Language identity.</param>
		[UrlSubAction("Index")]
		[UrlRoute("{id}/Delete", Path="Admin/Languages")]
		[UrlRouteConstraint("id", Constraints.IsInt)]
		public ActionResult Delete(int id)
		{
			// get culture
			Language language = Data.Get<Language>(id);
			if (language == null)
				return HttpNotFound();

			// delete culture
			using (ITransaction t = Data.BeginTransaction())
			{
				Data.Delete(language);
				t.Commit();
			}
			
			return RedirectToAction("Index");
		}
	}
}
